package com.lancoo.ccas53.entity;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.lancoo.ccas53.pojo.vo.Building;
import com.lancoo.ccas53.pojo.vo.RoomCollegeTreeVo;
import com.lancoo.ccas53.pojo.vo.RoomVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 教室
 * </p>
 *
 * @author quin.song
 * @since 2024-05-20
 */
@Data
@Accessors(chain = true)
@ApiModel(value = "Room对象", description = "教室")
@EqualsAndHashCode(of = {"taskId", "baseRoomId"})
public class Room implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "room_id", type = IdType.AUTO)
    private Long roomId;

    private Integer taskId;

    private Integer baseRoomId;

    private String roomName;

    @ApiModelProperty("教室所属（学院：写入college_id）")
    private String baseCollegeId;

    private String collegeName;

    private Integer buildingFloor;

    private Integer buildingId;

    private String buildingName;

    @ApiModelProperty("基础教室类型，仅供参考")
    private Integer baseRoomType;

    @ApiModelProperty("场地类型")
    @TableField(value = "room_type")
    private Integer roomType;

    @ApiModelProperty("场地类型")
    @TableField(value = "room_type_name")
    private String roomTypeName;

    @ApiModelProperty("容量（不可为空）")
    private Integer roomNum;

    @ApiModelProperty("所属校区")
    private Integer campusId;

    private String campusName;

    @ApiModelProperty("是否可用 0未启用 1启用")
    private Integer isEnable;

    @ApiModelProperty("同时上课班级数")
    @TableField("concurrent_class")
    private Integer concurrentClass;

    @ApiModelProperty("是否设置规则 1：已设置，2未设置")
    private Integer ruleSet;

    @ApiModelProperty("备注")
    private String remark;

    @ApiModelProperty("备注")
    private String baseSchemeId;

    @ApiModelProperty("备注")
    private String baseSchemeName;

    @TableField(exist = false)
    @ApiModelProperty("排课状态")
    private Boolean scheduleState = true;

    public static List<RoomCollegeTreeVo> toRoomTree(List<Room> roomList) {
        List<RoomCollegeTreeVo> roomCollegeTreeVoList = new ArrayList<>();

        roomList.forEach(o -> {
            if (StrUtil.isEmpty(o.getBaseCollegeId())) {
                o.setBaseCollegeId("000");
                o.setCollegeName("未知学院");
            }
        });


        for (Room room : roomList) {
            String baseCollegeId = room.getBaseCollegeId();
            String collegeName = room.getCollegeName();
            RoomCollegeTreeVo roomCollegeTreeVo = null;
            for (RoomCollegeTreeVo o : roomCollegeTreeVoList) {
                if (Objects.equals(o.getBaseCollegeId(), baseCollegeId)) {
                    roomCollegeTreeVo = o;
                    break;
                }
            }
            if (roomCollegeTreeVo == null) {
                roomCollegeTreeVo = new RoomCollegeTreeVo();
                roomCollegeTreeVo.setBaseCollegeId(baseCollegeId);
                roomCollegeTreeVo.setCollegeName(collegeName);
                roomCollegeTreeVo.setCampusList(new ArrayList<RoomVo>());
                roomCollegeTreeVoList.add(roomCollegeTreeVo);
            }

            Integer campusId = room.getCampusId();
            String campusName = room.getCampusName();

            RoomVo campus = null;
            for (RoomVo c : roomCollegeTreeVo.getCampusList()) {
                if (c.getCampusId().equals(campusId)) {
                    campus = c;
                    break;
                }
            }
            if (campus == null) {
                campus = new RoomVo();
                campus.setCampusId(campusId);
                campus.setCampusName(campusName);
                campus.setBuildings(new ArrayList<Building>());
                roomCollegeTreeVo.getCampusList().add(campus);
            }

            int buildingId = room.getBuildingId();
            String buildingName = room.getBuildingName();
            Building building = null;
            for (Building b : campus.getBuildings()) {
                if (b.getBuildingId() == buildingId) {
                    building = b;
                    break;
                }
            }
            if (building == null) {
                building = new Building();
                building.setBuildingId(buildingId);
                building.setBuildingName(buildingName);
                building.setRooms(new ArrayList<Room>());
                campus.getBuildings().add(building);
            }

            building.getRooms().add(room);
            building.setRoomSum(building.getRoomSum() + 1);
            campus.setRoomSum(campus.getRoomSum() + 1);
            roomCollegeTreeVo.setRoomSum(roomCollegeTreeVo.getRoomSum() + 1);

            building.setScheduleState(room.getScheduleState() ? building.getScheduleState() : false);
            campus.setScheduleState(room.getScheduleState() ? campus.getScheduleState() : false);
            roomCollegeTreeVo.setScheduleState(room.getScheduleState() ? roomCollegeTreeVo.getScheduleState() : false);
        }
        return roomCollegeTreeVoList;
    }

    public static List<RoomVo> toRoomTree2(List<Room> roomList) {
        List<RoomVo> roomTreeVoList = new ArrayList<>();
        for (Room room : roomList) {
            Integer campusId = room.getCampusId();
            String campusName = room.getCampusName();

            RoomVo campus = null;
            for (RoomVo c : roomTreeVoList) {
                if (c.getCampusId().equals(campusId)) {
                    campus = c;
                    break;
                }
            }
            if (campus == null) {
                campus = new RoomVo();
                campus.setCampusId(campusId);
                campus.setCampusName(campusName);
                campus.setBuildings(new ArrayList<Building>());
                roomTreeVoList.add(campus);
            }

            int buildingId = room.getBuildingId();
            String buildingName = room.getBuildingName();
            Building building = null;
            for (Building b : campus.getBuildings()) {
                if (b.getBuildingId() == buildingId) {
                    building = b;
                    break;
                }
            }
            if (building == null) {
                building = new Building();
                building.setBuildingId(buildingId);
                building.setBuildingName(buildingName);
                building.setRooms(new ArrayList<Room>());
                campus.getBuildings().add(building);
            }

            building.getRooms().add(room);
            building.setRoomSum(building.getRoomSum() + 1);
            campus.setRoomSum(campus.getRoomSum() + 1);

            building.setScheduleState(room.getScheduleState() ? building.getScheduleState() : false);
            campus.setScheduleState(room.getScheduleState() ? campus.getScheduleState() : false);
        }
        return roomTreeVoList;
    }
}
